#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "i2c_custom.h"
#include "lv_port.h"
#include "nvs_storage_hal.h"
#include "spiffs_hal.h"
#include "max30102_app.h"
#include "pcf8563.h"
#include "axp2101.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_now_hal.h"
#include "protocol_examples_common.h"

static const char *TAG = "main";

// // Dimensions of the buffer that the task being created will use as its stack.
// // NOTE:  This is the number of words the stack will hold, not the number of
// // bytes.  For example, if each stack item is 32-bits, and this is set to 100,
// // then 400 bytes (100 * 32-bits) will be allocated.
// #define STACK_SIZE 1024 * 4

// // Structure that will hold the TCB of the task being created.
// StaticTask_t xTaskBuffer;

// // Buffer that the task being created will use as its stack.  Note this is
// // an array of StackType_t variables.  The size of StackType_t is dependent on
// // the RTOS port.
// StackType_t xStack[STACK_SIZE];

static void dump_haps(void *arg)
{
    while (1)
    {
        size_t size = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
        size_t size_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
        ESP_LOGW(TAG, "RAM FREE SIZE --> PSRAM size=%d, SRAM sizeinternal=%d", size, size_internal);
        vTaskDelay(pdMS_TO_TICKS(5000));
    }
}

void app_main(void)
{
    /* sys init start */
    // nvs_init();
    i2c_init();
    vTaskDelay(pdMS_TO_TICKS(100));
    /* sys init end */
    xTaskCreate(dump_haps, "dump_haps", 1024 * 4, NULL, 3, NULL);
    xTaskCreate(max30102_task, "max30102_task", 1024 * 4, NULL, 4, NULL);
    xTaskCreate(pcf8563_task, "pcf8563_task", 1024 * 4, NULL, 5, NULL);
    xTaskCreate(axp2101_task, "axp2101_task", 1024 * 4, NULL, 6, NULL);
    xTaskCreate(esp_now_task, "esp_now_task", 1024 * 4, NULL, 7, NULL);
    xTaskCreate(lv_gui_task, "gui", 1024 * 10, NULL, 8, NULL);
    /* freertos create task end */
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}